import { ref } from 'vue'
import { storeToRefs } from "pinia"
import { userInfo } from "@/api/app/index"
import { deptList, userList, contactTitleList, search_month, intentionLevelList, visit_cooperation_List, enterpriseList, enterpriseSourceList } from "@/api/common/index"
import { useUserInfoStore } from "@/store/useUserInfoStore";
const userInfoStore = useUserInfoStore();
const { permissions } = storeToRefs(userInfoStore); // 保持响应式
function useViewModel() {

	//获取对接公司字典
	let deptList_data = ref([])
	let getDeptList = () : void => {
		deptList().then(res => {
			let { data } = res as any
			deptList_data.value = data.map(item => {
				item.value = String(item.deptId)
				item.text = item.deptName
				return item
			})
		})
	}

	//获取团队人员字典
	let teamInfo_data = ref([])
	let getTeamInfo = (deptId ?: string | number) : void => {
		userList(deptId || '').then(res => {
			let { rows } = res as any
			teamInfo_data.value = rows
		})
	}

	//获取职务字典
	let contactTitleList_data = ref([])
	let getContactTitleList = () => {
		contactTitleList().then(res => {
			let { data } = res as any
			contactTitleList_data.value = data.map(item => {
				item.value = item.dictValue
				item.text = item.dictLabel
				return item
			})
		})
	}

	//获取月份字典
	let searchMonthList_data = ref([])
	let getSearchMonthList = () => {
		search_month().then(res => {
			let { data } = res as any
			searchMonthList_data.value = data.map(item => {
				item.value = item.dictValue
				item.text = item.dictLabel
				return item
			})
		})
	}

	//意向度字典
	let intentionLevelList_data = ref([])
	let getIntentionLevelList = () => {
		intentionLevelList().then(res => {
			let { data } = res as any
			intentionLevelList_data.value = data.map(item => {
				item.value = item.dictValue
				item.text = item.dictLabel
				return item
			})
		})
	}


	//合作状态
	let visit_cooperation_List_data = ref([])
	let getVisit_cooperation_List = () => {
		visit_cooperation_List().then(res => {
			let { data } = res as any
			visit_cooperation_List_data.value = data.map(item => {
				item.value = item.dictValue
				item.text = item.dictLabel
				return item
			})
		})
	}
	
	//获取企业类型字典
	let enterpriseList_data = ref([])
	let getEnterpriseList = (): void => {
		enterpriseList().then(res => {
			let { data } = res as any
			enterpriseList_data.value = data.map(item => {
				item.value = item.dictValue
				item.text = item.dictLabel
				return item
			})
		})
	}
	
	//企业来源字典
	let enterprise_source_data = ref([])
	let getEnterpriseSourceList = (): void => {
		enterpriseSourceList().then(res => {
			let { data } = res as any
			enterprise_source_data.value = data.map(item => {
				item.value = item.dictValue
				item.text = item.dictLabel
				return item
			})
		})
	}
	
	//删除数据
	/**
	 * api: 接口
	 * id: 删除的数据id
	 * delta: 返回页面的页数
	 */
	let deleteItem = (api: any, id: string, delta: number) => {
		uni.showModal({
			content: '是否确认删除？',
			title: '提示',
			cancelText: '否',
			confirmText: '是',
			success: function (res) {
					if (res.confirm) {
						api(id).then(res => {
							let { code } = res
							if(code == 200){
								uni.showToast({
									title: '删除成功',
								})
								
								setTimeout(() => {
									uni.navigateBack({
										delta
									})
								}, 1000)
							}
						})
					} else if (res.cancel) {
						
					}
				}
		})
		
	}
	
	//权限
	let hasPermission = (str: string): boolean => {
		return !!(permissions.value.find(item => item == str))
	}
	
	//获取用户信息
	let getUserInfo = () => {
		return new Promise((resolve, _reject) => {
			userInfo().then(res => {
				let { permissions, user } = res as any
				userInfoStore.setPermissions(permissions)
				userInfoStore.setUserInfo(user)
				resolve('')
			})
		})
		
	}

	return {
		getDeptList,
		deptList_data,
		getTeamInfo,
		teamInfo_data,
		getContactTitleList,
		contactTitleList_data,
		getSearchMonthList,
		searchMonthList_data,
		getIntentionLevelList,
		intentionLevelList_data,
		getVisit_cooperation_List,
		visit_cooperation_List_data,
		getEnterpriseList,
		enterpriseList_data,
		getEnterpriseSourceList,
		enterprise_source_data,
		deleteItem,
		hasPermission,
		getUserInfo
	}
}

export default useViewModel